﻿using CMWVS.Core;
using EnvDTE80;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace bleissem.CMWVS.UI
{
    public class ToOutputWindow
    {
        #region Constructor

        private ToOutputWindow()
        {
        }

        public ToOutputWindow(Resolver resolver)
        {
            m_Resolver = resolver;
        }

        #endregion Constructor

        private Resolver m_Resolver;

        private EnvDTE.OutputWindow GetOutPutWindow()
        {
            DTE2 dte2 = this.m_Resolver.Get<DTE2>();
            EnvDTE.OutputWindow outputwindow = dte2.ToolWindows.OutputWindow;
            return outputwindow;
        }

        public void Clear()
        {
            var outputwindow = GetOutPutWindow();

            if ((null != outputwindow) && (null != outputwindow.ActivePane))
            {
                outputwindow.ActivePane.Activate();
                outputwindow.ActivePane.Clear();
            }
        }

        public void OutPut(string message)
        {
            var outputwindow = GetOutPutWindow();

            if ((null != outputwindow) && (null != outputwindow.ActivePane))
            {
                using (BackgroundWorker backgroundworker = new BackgroundWorker())
                {
                    backgroundworker.DoWork += new DoWorkEventHandler(
                        (object sender, DoWorkEventArgs e) =>
                        {
                            outputwindow.ActivePane.OutputString(message);
                        });
                    backgroundworker.RunWorkerAsync();
                }
            }
        }
    }
}